package com.scope.customViews;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.scope.R;
import com.scope.tasks.ImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by DELL on 10/5/13.
 */
public abstract class FileExplorerAdapter<FileButton extends ImageButton, FileLoader extends ImageLoader> extends BaseAdapter implements CustomView.IAccent {
    public FileExplorerAdapter(Context context) {
        mContext = context;
        mFolderRetriever = true;
        mFileLoader = createFileLoader();
    }

    private Context mContext;
    private FileLoader mFileLoader;

    protected Context getContext() {
        return mContext;
    }

    public int getSize() {
        return mFilePaths.size() + mFolderPaths.size();
    }

    public int getCount() {
        if (mIsEmpty) {
            return 1;
        } else {
            return mFilePaths.size() + mFolderPaths.size();
        }
    }

    public Object getItem(int position) {
        if (position < mFolderCount) {
            return mFolderPaths.get(position);
        } else {
            position -= mFolderCount;
            return mFilePaths.get(position);
        }
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (mIsEmpty) {
            LabelButton folderButton;
            if (convertView == null) {
                folderButton = createFolderButton();
                setEmptyButtonProp(folderButton);
            } else {
                folderButton = (LabelButton) convertView;
            }
            return folderButton;
        } else if (position < mFolderCount) {
            LabelButton folderButton;
            if (convertView == null) {
                folderButton = createFolderButton();
                setFolderButtonProp(folderButton);
            } else {
                folderButton = (LabelButton) convertView;
            }
            folderButton.setLabel((new File(mFolderPaths.get(position))).getName());
            return folderButton;
        } else {
            position -= mFolderCount;
            FileButton fileButton;
            if (convertView == null) {
                fileButton = createFileButton();
                setFileButtonProp(fileButton);
            } else {
                fileButton = (FileButton) convertView;
            }
            mFileLoader.load(mFilePaths.get(position), fileButton);
            return fileButton;
        }
    }

    public boolean isDirectory(int position) {
        if (mIsEmpty) {
            return false;
        } else {
            return position < mFolderCount;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mIsEmpty) {
            return 0;
        } else if (position < mFolderCount) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    protected abstract LabelButton createFolderButton();

    protected abstract FileButton createFileButton();

    protected abstract FileLoader createFileLoader();

    protected abstract void setFileButtonProp(FileButton fileButton);

    private void setFolderButtonProp(LabelButton folderButton) {
        folderButton.setOrientation(CustomView.Orientation.VERTICAL);
        folderButton.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_folder));
        folderButton.setAccent(mTheme);
    }

    private void setEmptyButtonProp(LabelButton folderButton) {
        folderButton.setOrientation(CustomView.Orientation.VERTICAL);
        folderButton.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_empty));
        folderButton.setLabel("Empty");
        folderButton.setAccent(mContext.getResources().getColor(R.color.Red));
        folderButton.setChecked(false);
    }

    @Override
    public boolean isEnabled(int position) {
        if (mIsEmpty) {
            return false;
        } else {
            return super.isEnabled(position);
        }
    }

    private int mTheme;

    public void setAccent(int theme) {
        mTheme = theme;
    }

    public int getAccent() {
        return mTheme;
    }

    protected String mRootPath;

    public String getRootPath() {
        return mRootPath;
    }

    public void setRootPath(String path) {
        mRootPath = path;
        retrieveFiles();
        mFolderCount = mFolderPaths.size();
        if (mFolderCount + mFilePaths.size() == 0) {
            mIsEmpty = true;
        } else {
            mIsEmpty = false;
        }

        Log.d(toString(), mRootPath);
        notifyDataSetChanged();
    }

    @Override
    public String toString() {
        return super.toString();
    }

    private int mFolderCount = 0;
    public int getFolderCount() {
        return mFolderCount;
    }

    private boolean mIsEmpty = false;
    protected List<String> mFilePaths = new ArrayList<String>();
    public List<String> getFilePaths() {
        return mFilePaths;
    }

    protected List<String> mFolderPaths = new ArrayList<String>();
    public List<String> getFolderPaths() {
        return mFolderPaths;
    }

    protected void retrieveFiles() {
        mFilePaths.clear();
        mFolderPaths.clear();

        if (mRootPath == null || mRootPath.length() == 0) {
            retrieveBaseFiles();
        } else {
            try {
                File dir = new File(mRootPath);
                File[] files = dir.listFiles();

                for (File file : files) {
                    if (file.isDirectory() && isNecessaryDirectory(file)) {
                        mFolderPaths.add(file.getAbsolutePath());
                    } else if (file.isFile() && isTargetedFile(file)) {
                        mFilePaths.add(file.getAbsolutePath());
                    } else {
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    protected abstract void retrieveBaseFiles();

    private boolean mFolderRetriever;

    public boolean getFolderRetriever() {
        return mFolderRetriever;
    }

    public void setFolderRetriever(boolean folder) {
        mFolderRetriever = folder;
    }

    private Boolean isNecessaryDirectory(File file) {
        if (mFolderRetriever && file.exists() && file.canRead() && !file.isHidden()) {
            return true;
        } else {
            return false;
        }
    }

    protected abstract Boolean isTargetedFile(File file);
}